﻿Imports GT = Gadgeteer
Imports GTM = Gadgeteer.Modules
Imports Gadgeteer.Modules.GHIElectronics
Imports Gadgeteer.Modules.Edu.Ghi


Namespace GadgeteerApp1
    Partial Public Class Program

        Dim melody As Melody = New Melody()
        Dim amountLight As Double


        ' This is run when the mainboard is powered up or reset. 
        Public Sub ProgramStarted()
            ' Use Debug.Print to show messages in Visual Studio's "Immediate" window during debugging.
            Debug.Print("Program Started")
            'Create a melody that will run then an intruder is detected. 
            melody.Add(800, 300)
            melody.Add(300, 300)
            melody.Add(700, 300)
            melody.Add(200, 300)

            'Clear the led strip
            led_Strip.TurnAllLedsOff()


            timer.Start()

        End Sub

        ' If you want to do something periodically, declare a GT.Timer by uncommenting the below line
        '   and then use the dropdown boxes at the top of this window to generate a Tick event handler.
        Dim WithEvents timer As GT.Timer = New GT.Timer(3000)  ' every 3 seconds (3000ms)


        Private Sub timer_Tick(timer As Gadgeteer.Timer) Handles timer.Tick
            'Check the light sensor levels
            amountLight = lightSensor.GetIlluminance()
            Debug.Print("illuminence :" + amountLight.ToString()) 'Print the value to the debug window

            'If the alarm is too sensitive, change the alarm level (default 50)
            If amountLight > 50 Then
                Debug.Print("Intruder detected")
                tunes.Play(melody)

                'Set LED alert on
                led_Strip.SetLED(6, True) 'RED On
                led_Strip.SetLED(5, True)
                led_Strip.SetLED(0, False) 'Green off
                led_Strip.SetLED(1, False)



            Else
                Debug.Print("No intruder detected, all OK")
                'Set LED alert off
                led_Strip.SetLED(6, False) 'RED off
                led_Strip.SetLED(5, False)
                led_Strip.SetLED(0, True) 'Green on
                led_Strip.SetLED(1, True)
                tunes.Stop() ' Turn off an alarm incase it was triggered.
            End If

        End Sub

        Private Sub button_ButtonPressed(sender As Gadgeteer.Modules.GHIElectronics.Button, state As Gadgeteer.Modules.GHIElectronics.Button.ButtonState) Handles button.ButtonPressed
            If timer.IsRunning Then
                timer.Stop() 'Stop the trigger timer
                tunes.Stop() 'Turn off the alarm if it is making a noise                
                Debug.Print("Alarm disabled")
            Else
                timer.Start()
                Debug.Print("Alarm enabled")
            End If
        End Sub
    End Class
End Namespace